package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.AdminCategoryService;

import com.cskaoyan.market.service.impl.AdminCategoryServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import com.fasterxml.jackson.databind.ObjectMapper;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


//701	插入失败
//702	更新失败
//703	删除失败
//704


@WebServlet("/admin/category/*")
public class AdminCategoryController extends HttpServlet {
    AdminCategoryService adminCategoryService =new AdminCategoryServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op=requestURI.replace(req.getContextPath()+"/admin/category/","");
        if(op.equals("delete")){
            delete(req,resp);
        }
        if(op.equals("update")){
            update(req,resp);
        }
        if(op.equals("create")){
            create(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketCategory marketCategory = objectMapper.readValue(req.getReader().readLine(), MarketCategory.class);
        int update = adminCategoryService.update(marketCategory);
        if(update==1){
            Object ok = ResponseUtil.ok(marketCategory);
            resp.getWriter().println(objectMapper.writeValueAsString(ok));
            return ;
        }
        resp.getWriter().println(objectMapper.writeValueAsString(ResponseUtil.badUpdate()));

    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {


        MarketCategory marketCategory = JacksonUtil.getObjectMapper().readValue(req.getReader().readLine(), MarketCategory.class);
        int effectRows= adminCategoryService.create(marketCategory);
        if(effectRows==1){
            Object ok = ResponseUtil.ok(marketCategory);
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
            return ;
        }
        Object fail = ResponseUtil.badCreate();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(fail));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String reqJson = req.getReader().readLine();
        MarketCategory marketCategory = objectMapper.readValue(reqJson, MarketCategory.class);
        int effectRows = adminCategoryService.delete(marketCategory.getId());
        if(effectRows==1){

            Object ok = ResponseUtil.ok();
            resp.getWriter().write(objectMapper.writeValueAsString(ok));
            return;
        }
        Object fail = ResponseUtil.badDelete();
        resp.getWriter().write(objectMapper.writeValueAsString(fail));

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op=requestURI.replace(req.getContextPath()+"/admin/category/","");
        if(op.equals("list")){
            list(req,resp);
        }
        if(op.equals("l1")){
            l1(req,resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int limit=10;
        int page=1;
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();

        resp.getWriter().println(adminCategoryService.list());
    }






    private void l1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int limit=10;
        int page=1;

        List<HashMap<String,String>> list = adminCategoryService.l1(limit, page);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        if(list!=null && !list.isEmpty()){
            Object okList = ResponseUtil.okList(list);
            String respJson = objectMapper.writeValueAsString(okList);
            resp.getWriter().println(respJson);
            return ;
        }
        Object fail = ResponseUtil.badList();
        String s = objectMapper.writeValueAsString(fail);
        resp.getWriter().println(s);
    }
}
